package tan.dft.quanly;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import tan.dft.gps.Danduong;
import tan.dft.gps.R;
import tan.dft.gps.ShowMaps;
import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class Qlbiquanlymain extends Activity {
	private ListView listquanly;
	private ArrayList<Qlbiquanly> array;
	private QlbiquanlyAdapter adapter;
	private List<Address> addresses;
	private Geocoder geocoder;
	private int idbql1, age;
	private Double lat, lng;
	private String myjsonstring, name, gender, add;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.quanly);

		listquanly = (ListView) findViewById(R.id.listquanly);
		array = new ArrayList<Qlbiquanly>();
		adapter = new QlbiquanlyAdapter(Qlbiquanlymain.this,
				R.layout.biquanlyitem, array);
		createArray();
		listquanly.setAdapter(adapter);

		Button btshowall = (Button) findViewById(R.id.btshowall);
		btshowall.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent().setClass(getBaseContext(),
						Danduong.class);
				ArrayList<Qlbiquanly> addyExtras = new ArrayList<Qlbiquanly>();
				for (int i = 0; i < array.size(); i++) {
					addyExtras.add(array.get(i));
				}
				intent.putParcelableArrayListExtra("mylist", addyExtras);
				startActivity(intent);
			}
		});

		listquanly.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// + dinh nghia 1 intent
				Intent intent = new Intent(Qlbiquanlymain.this, ShowMaps.class);
				// + dinh nghia 1 bunble
				Bundle bundle = new Bundle();
				// + dat cac gia tri can truyen vao bunble
				bundle.putInt("idbql1", array.get(arg2).getIdbql1());
				bundle.putString("name", array.get(arg2).getName());
				bundle.putDouble("lat", array.get(arg2).getLat());
				bundle.putDouble("lng", array.get(arg2).getLng());
				// + dat bundle vao truong extra cua intent
				intent.putExtras(bundle);
				// + start Activity
				startActivity(intent);
			}

		});
	}

	private void createArray() {

		// Reading text file from assets folder
		StringBuffer sb = new StringBuffer();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(getAssets().open(
					"jsondata.txt")));
			String temp;
			while ((temp = br.readLine()) != null)
				sb.append(temp);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close(); // stop reading
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		myjsonstring = sb.toString();
		try {
			// Creating JSONObject from String
			JSONObject jsonObjMain = new JSONObject(myjsonstring);
			// Creating JSONArray from JSONObject
			JSONArray jsonArray = jsonObjMain.getJSONArray("data");
			// JSONArray has four JSONObject
			for (int i = 0; i < jsonArray.length(); i++) {
				// Creating JSONObject from JSONArray
				JSONObject jsonObj = jsonArray.getJSONObject(i);
				// Getting data from individual JSONObject
				final int idmay = jsonObj.getInt("idmay");

				JSONArray jsonArraybql = jsonObj.getJSONArray("idbql");
				for (int k = 0; k < jsonArraybql.length(); k++) {
					JSONObject jsonObjectbql = jsonArraybql.getJSONObject(k);
					idbql1 = jsonObjectbql.getInt("idbql1");
					name = jsonObjectbql.getString("name");
					gender = jsonObjectbql.getString("gender");
					age = jsonObjectbql.getInt("age");
					JSONObject jObj = jsonObjectbql.getJSONObject("toado");
					lat = jObj.getDouble("lat");
					lng = jObj.getDouble("lng");

					geocoder = new Geocoder(this, Locale.getDefault());
					try {
						addresses = geocoder.getFromLocation(lat, lng, 1);
						String address = addresses.get(0).getAddressLine(0);
						String city1 = addresses.get(0).getAddressLine(1);
						String country = addresses.get(0).getAddressLine(2);
						add = address + " | " + city1 + " | " + country;

					} catch (IOException e) {
						e.printStackTrace();
					}

					Qlbiquanly quanly = new Qlbiquanly(idmay, idbql1, age,
							name, add, gender, lat, lng);
					array.add(quanly);
				}
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

}
